#/usr/bin/perl                                                                                                                           

# Usage: perl compute.grid.genetic.pattern.across.time.pl 1 100 - run sweeps 1 to 100
# Input:  .log file, .patterns file
# output: .grid.log              date, generation, crypt_id, clone_id, pattern_id,
# output: .unique.patterns       pattern_id, states of neutral loci 1..n
# output: .clones.to.patterns    sorted clone_id, pattern_id 

$locitot = 100; # 100 total loci
$locitot = $locitot-1; # from 0 to 99 -> 100 loci total
$name = "sim";

my %patterns;
for $sw ($ARGV[0]..$ARGV[1]){

    %patterns=();

    # Load all patterns in memory
    open (in,"$name$sw/$name$sw.patterns");
    while(<in>){
	chomp;
	@row=split(/\,/);
	$patterns{$row[0]}="";
	for my $loc (0..$locitot){
	    # Add to an array
	    # Pattern is locus_id.ms_length.locus_id.ms_length
	    # locus is 0 through 99
	    # where length 0 is ignored
	    if($row[$loc+1] ne "0"){
		$patterns{$row[0]}=$patterns{$row[0]}.($loc).",".$row[$loc+1].",";
	    }
	}
	# For a wild-type pattern, add it to the patterns hash
	if($patterns{$row[0]} eq ""){
	    $patterns{$row[0]}=" ";
	}
	#print $row[0]."-".$patterns{$row[0]}."\n";
    }
    close in;
    #
    print STDERR "Sim $sw patterns loaded.\n";
    # Dead cells have cid=-1 so they have no pattern
    $patterns{-1}="X";
    
    # Store ms pattern at each time step 
    %pathash=();
    # Hash of key = clone_id, value = unique_pattern_id
    %cidhash=();
    $pat_count = 0;
    open (i1,"$name$sw/$name$sw.log");
    open (out, ">$name$sw/$name$sw.grid.log");
    open (out2, ">$name$sw/$name$sw.unique.patterns");
    my $day = 182;
    while(<i1>){
	chomp;
	@row=split(/\,/);
	if($row[2]==$day){
	    
	    $cid = $row[1];
	    # get the pattern from hash
	    # Here we assign a unique id for every unique pattern
	    # where two clones with different clone_ids can share the same pattern
	    # and therefore that pattern will have the same unique id.
	    $pat = $patterns{$cid};
	    if($pat ne "X"){
		if(!defined($pathash{$pat})){
		    $pat_count++;
		    $pathash{$pat}=$pat_count;		
		    # Output the pattern in .grid.patterns
		    # pat_id, pattern
		    my $pat_arr;
		    for my $loc (0..$locitot){
			$pat_arr[$loc] = 0;
		    }
		    # If pattern is not wild-type
		    if($pat ne " "){		
			@arr = split(/\,/,$pat);
			for my $loc (0..$#arr){
			    if($loc%2==0){
				$pat_arr[$arr[$loc]] = $arr[$loc+1];
			    }
			}    
		    }
		    # Print pattern id, pattern
		    print out2 $pat_count.",";
		    for my $loc (0..($locitot-1)){
			print out2 $pat_arr[$loc].",";
		    }
		    print out2 $pat_arr[$locitot]."\n";
		}  
		$cidhash{$cid}=$pathash{$pat};
	    }
	    # date, generation, crypt_id, cid, pat_id, 
	    print out $row[2].",".$row[3].",".$row[0].",".$cid.",".$pathash{$pat}."\n";    
	} elsif($row[2]==$day+182) {
	    $day +=182;	    
	    print STDERR "Timepoint $day processed\n";
	    $cid = $row[1];
	    # get the pattern from hash
	    # Here we assign a unique id for every unique pattern
	    # where two clones with different clone_ids can share the same pattern
	    # and therefore that pattern will have the same unique id.
	    $pat = $patterns{$cid};
	    if($pat ne "X"){
		if(!defined($pathash{$pat})){
		    $pat_count++;
		    $pathash{$pat}=$pat_count;
		    # Output the pattern in .grid.patterns
		    # pat_id, pattern
		    my $pat_arr;
		    for my $loc (0..$locitot){
			$pat_arr[$loc] = 0;
		    }
		    # If pattern is not wild-type
		    if($pat ne " "){		
			@arr = split(/\,/,$pat);
			for my $loc (0..$#arr){
			    if($loc%2==0){
				$pat_arr[$arr[$loc]] = $arr[$loc+1];
			    }
			}
		    }
		    # Print pattern id, pattern
		    print out2 $pat_count.",";
		    for my $loc (0..($locitot-1)){
			print out2 $pat_arr[$loc].",";
		    }
		    print out2 $pat_arr[$locitot]."\n";
		}
		$cidhash{$cid}=$pathash{$pat};
	    }	    	    
	    # date, generation, crypt_id, cid, pat_id, 
	    print out $row[2].",".$row[3].",".$row[0].",".$cid.",".$pathash{$pat}."\n";    
	} else {
	    $day = 7300;	    
	    print STDERR "Timepoint $day processed\n";
	    $cid = $row[1];
	    # get the pattern from hash
	    # Here we assign a unique id for every unique pattern
	    # where two clones with different clone_ids can share the same pattern
	    # and therefore that pattern will have the same unique id.
	    $pat = $patterns{$cid};
	    if($pat ne "X"){
		if(!defined($pathash{$pat})){
		    $pat_count++;
		    $pathash{$pat}=$pat_count;
		    # Output the pattern in .grid.patterns
		    # pat_id, pattern
		    my $pat_arr;
		    for my $loc (0..$locitot){
			$pat_arr[$loc] = 0;
		    }
		    # If pattern is not wild-type
		    if($pat ne " "){				    
			@arr = split(/\,/,$pat);
			for my $loc (0..$#arr){
			    if($loc%2==0){
				$pat_arr[$arr[$loc]] = $arr[$loc+1];
			    }
			}
		    }
		    # Print pattern id, pattern
		    print out2 $pat_count.",";
		    for my $loc (0..($locitot-1)){
			print out2 $pat_arr[$loc].",";
		    }
		    print out2 $pat_arr[$locitot]."\n";
		}
		$cidhash{$cid}=$pathash{$pat};
	    }
	    # date, generation, crypt_id, cid, pat_id, 
	    print out $row[2].",".$row[3].",".$row[0].",".$cid.",".$pathash{$pat}."\n";    
	}
    }
    close i1;    
    close out;
    close out2;
    
    open (out, ">$name$sw/$name$sw.clones.to.patterns");
    for my $cloneid (sort {$a <=> $b} keys %cidhash) {
	print out $cloneid.",".$cidhash{$cloneid}."\n";
    }
    close out;
}

    
